/**
* 格式化时间 
* @param {String} date 原始时间格式
* 格式后的时间：yyyy/mm/dd hh:mm:ss
**/
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

/**
* 格式化时间 
* @param {String} date 原始时间格式
* 格式后的时间：hh:mm
**/
const formatDateTime = date => {
  const hour = date.getHours()
  const minute = date.getMinutes()

  return [hour, minute, second].map(formatNumber).join(':')
}

/**
* 格式化时间 
* @param {String} date 原始时间格式
* 格式后的时间：yyyy-mm-dd
**/
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
* 获取指定日期前后N天的日期、前N个月日期、后N个月日期
* @param {String} sdate 当前日期
* @param {Number} interval 间隔天数
* @param {String} caret 间隔符号
* @example 获取当天日期 getNowFormatDate("",0,"-"); 结果为"2018-09-18";
           获取前一天日期  getNowFormatDate("2018-03-01",-1,"-"); 结果为"2018-02-18";
           获取后一天日期  getNowFormatDate("2018-02-28",1,"-"); 结果为"2018-03-01";
**/
function getNowFormatDate(sdate, interval, caret) {
  var patt1 = /^\d{4}-([0-1]?[0-9])-([0-3]?[0-9])$/;  //判断输入的日期是否符合格式正则表达式
  if (!(sdate && typeof (sdate) == "string" && patt1.test(sdate))) {
    sdate = new Date(); //不满足日期的则使用当前年月日
  }
  interval = isNaN(parseInt(interval)) ? 0 : parseInt(interval);//若没有输入间隔，则使用当前日
  caret = (caret && typeof (caret) == "string") ? caret : "";
  var gdate = new Date(sdate).getTime();//获取指定年月日
  gdate = gdate + 1000 * 60 * 60 * 24 * interval; //加减相差毫秒数
  var speDate = new Date(gdate);//获取指定好毫秒数时间
  var preYear = speDate.getFullYear();
  var preMonth = speDate.getMonth() + 1;
  var preDay = speDate.getDate();
  preMonth = (preMonth < 10) ? ("0" + preMonth) : preMonth;
  preDay = (preDay < 10) ? ("0" + preDay) : preDay;
  var preDate = preYear + caret + preMonth + caret + preDay;
  return preDate;
}


// 获取某年某月的有多少周
String.prototype.weekInMonthCount = function () {
  var date = new Date((new Date(this.replace(/-/g, "/"))) || (new Date()));
  var firstWeekDate = 1;// 默认第一周是本月1号  为了模拟本月1号是否为本月第1周的判断
  if (date.getDay() === 1) { // 判断1号是周一
    firstWeekDate = 1;
  } else if (date.getDay() === 0) { // 判断1号是周日
    firstWeekDate = 8 - 7 + 1;
  } else { // 判断1号是周二至周六之间
    firstWeekDate = 8 - date.getDay() + 1;
  }
  date.setMonth(date.getMonth() + 1);
  date.setDate(0);
  var monthHasDays = date.getDate();// 本月天数
  monthHasDays = date.getDate() - firstWeekDate + 1;
  var hasWeek = Math.ceil(monthHasDays / 7); // 计算本月有几周
  return hasWeek;
};

// 获取今天是第几周 注：ios不支持2018-01-14 需转化为2018/01/14
String.prototype.weekIndexInMonth = function () {
  var date_trim = (this.trim() != "" ? this : new Date()).replace(/-/g, "/");
  var date = new Date(date_trim);
  var dateStart_trim = new Date((this.trim() != "" ? this : new Date()).replace(/-/g, "/")).setDate(1);
  var dateStart = new Date(dateStart_trim); // 本月初
  var firstWeek = 1;
  if (dateStart.getDay() === 1) {
    firstWeek = 1;
  } else if (dateStart.getDay() === 0) {
    firstWeek = 8 - 7 + 1;
  } else {
    firstWeek = 8 - dateStart.getDay() + 1;
  }
  var weekIndex = 1;
  var c = date.getDate();
  if (date.getDay() === 1 && date.getDate() < 7) {
    weekIndex = 1;
  } else if (c < firstWeek) {
    weekIndex = -1;
  } else {
    if (c < 7) {
      weekIndex = Math.ceil(c / 7);
    } else {
      c = c - firstWeek + 1;
      if (c % 7 === 0) {
        if (dateStart.getDay() !== 6) {
          weekIndex = c / 7;
        } else {
          weekIndex = c / 7 + 1;
        }
      } else {
        weekIndex = Math.ceil(c / 7);
      }
    }
  }
  return weekIndex;
};

const getWeekByDate = dates => {
  let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  let date = new Date(dates);
  date.setDate(date.getDate());
  let day = date.getDay();
  return show_day[day];
 }

export {
  formatTime,
  formatDateTime,
  formatDate,
  formatNumber,
  getNowFormatDate,
  getWeekByDate
}